package entities;

import java.util.Vector;

import td.game.Main;
import td.game.MainGamePanel;
import util.Util;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Point;
import android.os.Handler;
import android.text.TextPaint;
import android.util.Log;
import android.view.MotionEvent;

/** Describes the Background map of the phase. It is composed by the map itself and the
 * the turret and turret points of the phase.
 * 
 * @author Bruno Zumba (22/09/2011)
 *
 */
public class BkgMap {
	private final String TAG = BkgMap.class.getSimpleName(); //Tag for Logging/debugging
	private final int WIDTH = Main.SCREEN_WIDTH; //Width and height of the map
	private final int HEIGHT = Main.SCREEN_WIDTH;
	
	private final double TILE_WIDTH = WIDTH/MainGamePanel.TILES_IN_X;
	private final double TILE_HEIGHT = WIDTH/MainGamePanel.TILES_IN_Y;
	
	private Bitmap bmp; //the actual bitmap of the map
	private Point pt; //the position this bmp is in the screen
	private Boolean touched; //indicates weather this bmp is touched

	private static Bitmap bmpTurret1lvl1;
	private static Bitmap bmpTurret1lvl2;
	private static Bitmap bmpTurret1lvl3;
	private static Bitmap bmpTurret2lvl1;
	private static Bitmap bmpTurret2lvl2;
	private static Bitmap bmpTurret2lvl3;
	private static Bitmap bmpTurretPoint;
	
	private Point last = new Point(); //used to check the last point the finger touched the screen
	
	private Vector<TurretPoint> turretPoints; //The vector containing all the turret points in the map
	private Vector<Turret> turrets; //The vector containing all the turrets in the map
	private TurretPoint selectedTP; //indicates which turret point is currently selected
	private Turret selectedTurret; //indicates which turret is currently selected
	
	private Handler insideHandler; //Handler to pass to the turrets and turret points
		
	
	public BkgMap(Bitmap bmp, Point pt, Bitmap bmpTurret1lvl1_p, Bitmap bmpTurret1lvl2_p, Bitmap bmpTurret1lvl3_p, 
			Bitmap bmpTurret2lvl1_p, Bitmap bmpTurret2lvl2_p, Bitmap bmpTurret2lvl3_p, 
			Bitmap bmpTurretPoint_p, Handler insideHandler){
		
		
		this.bmp = Util.resizeBitmap(bmp, WIDTH, HEIGHT);
		this.pt = new Point(pt.x, pt.y);
		touched = false;
		
		bmpTurret1lvl1 = bmpTurret1lvl1_p;
		Log.i(TAG, bmpTurret1lvl1.toString());
		bmpTurret1lvl2 = bmpTurret1lvl2_p;
		bmpTurret1lvl3 = bmpTurret1lvl3_p;
		bmpTurret2lvl1 = bmpTurret2lvl1_p;
		bmpTurret2lvl2 = bmpTurret2lvl2_p;
		bmpTurret2lvl3 = bmpTurret2lvl3_p;
		Log.i(TAG, bmpTurret1lvl1.toString());
		Log.i(TAG, bmpTurret1lvl2.toString());
		Log.i(TAG, bmpTurret1lvl3.toString());
		Log.i(TAG, bmpTurret2lvl1.toString());
		Log.i(TAG, bmpTurret2lvl2.toString());
		Log.i(TAG, bmpTurret2lvl3.toString());

		bmpTurretPoint = bmpTurretPoint_p;
		this.insideHandler = insideHandler;

		turretPoints = new Vector<TurretPoint>();
		turrets = new Vector<Turret>();

		addTurretPoint(new Point(2,2),0);
		addTurretPoint(new Point(5,2),0);
		addTurretPoint(new Point(8,2),0);
		addTurretPoint(new Point(1,4),0);
		addTurretPoint(new Point(3,5),0);
		addTurretPoint(new Point(7,6),0);
		addTurretPoint(new Point(2,7),0);
		addTurretPoint(new Point(4,8),0);
		addTurretPoint(new Point(8,8),0);
	}
	
	public static Bitmap getBitmap(int turret, int level){
		switch (turret){
		case Util.TURRET1:
			switch(level){
			case 1:
				return bmpTurret1lvl1;
			case 2:
				return bmpTurret1lvl2;
			case 3:
				return bmpTurret1lvl3;
			}
		break;
		case Util.TURRET2:
			switch(level){
			case 1:
				return bmpTurret2lvl1;
			case 2:
				return bmpTurret2lvl2;
			case 3:
				return bmpTurret2lvl3;
			}
		break;
		}
		
		return null;
		
	}
	
	public Bitmap getBitmap(){
		return bmp;
	}
	public void setBitmap(Bitmap bmp){
		this.bmp = bmp;
	}
	
	public Point getPoint(){
		return pt;
	}
	public void setPoint(Point pt){
		this.pt = pt;
	}
	
	public Boolean isTouched(){
		return touched;
	}
	public void setTouched(Boolean touched){
		this.touched = touched;
	}
	public Vector<Turret> getTurrets(){
		return turrets;
	}
	public Vector<TurretPoint>getTurretPoints(){
		return turretPoints;
	}
	
	public TurretPoint getSelectedTP(){
		return selectedTP;
	}
	public Turret getselectedTurret(){
		return selectedTurret;
	}
	
	public void draw(Canvas canvas) {
		canvas.drawBitmap(bmp, pt.x, pt.y, null);
		//canvas.drawText(String.valueOf(player), pt.x+relativePt.x, pt.y+relativePt.y+20, textPaint);
		TextPaint textPaint;
		textPaint = new TextPaint();
		textPaint.setColor(Color.WHITE);
		textPaint.setTextSize(30);

		int i;
		//Draw the elements in the map
		for(i = 0; i < turretPoints.size(); i++){
			turretPoints.elementAt(i).setRelativePoint(pt);
			if (turretPoints.elementAt(i) != null) turretPoints.elementAt(i).draw(canvas);
		}
		
		for(i = 0; i < turrets.size(); i++){
			turrets.elementAt(i).setRelativePoint(pt);
			turrets.elementAt(i).draw(canvas);
		}
		
	}

	public Boolean handleActionDown(MotionEvent ev) {
		//Test if other element was touched
		boolean elementTouched = false;
		for(int i = 0; i < turretPoints.size(); i++){
			turretPoints.elementAt(i).handleActionDown(ev);
			if (turretPoints.elementAt(i).isTouched()){
				selectedTP = turretPoints.elementAt(i); 
				break;
			}
		}
		
		for(int i = 0; i< turrets.size(); i++){
			turrets.elementAt(i).handleActionDown(ev);
			if(turrets.elementAt(i).isTouched()){
				selectedTurret = turrets.elementAt(i);
				break;
			}
		}
		
		if (!elementTouched) {
			//Handle the event based on the Action it executed
			switch (ev.getAction()) {
				case MotionEvent.ACTION_DOWN: //Touched the screen
					//Check weather or not the background was touched
					
					if (ev.getX() >= (pt.x) && (ev.getX() <= (pt.x + bmp.getWidth()))) {
						if (ev.getY() >= (pt.y) && (ev.getY() <= (pt.y + bmp.getHeight()))) {
							setTouched(true);
							
							//Update the last point the finger touched the screen
							last.x = (int)ev.getX();
							last.y = (int)ev.getY();
						} else {
							setTouched(false);
						}
					} else {
						setTouched(false);
					}
				break;
				
				case MotionEvent.ACTION_MOVE: //Moved the finger on the screen
					if (touched){
						//Update the position of the background map and the last point
						pt.x += ((int) ev.getX() - last.x);
						pt.y += ((int) ev.getY() - last.y);
						
						//Set the point of the bitmap so it doesn't leave the screen
						if (pt.x > MainGamePanel.BKG_MAP_RECT.left){
							pt.x = MainGamePanel.BKG_MAP_RECT.left;
						} else if (pt.x + bmp.getWidth() < MainGamePanel.BKG_MAP_RECT.right){
							pt.x = MainGamePanel.BKG_MAP_RECT.right - bmp.getWidth();
						}
						
						if (pt.y> MainGamePanel.BKG_MAP_RECT.top){
							pt.y =MainGamePanel.BKG_MAP_RECT.top;
						} else if (pt.y + bmp.getHeight() < MainGamePanel.BKG_MAP_RECT.bottom-75){
							pt.y = MainGamePanel.BKG_MAP_RECT.bottom - bmp.getHeight()-75; //75 is the pixels used by the title screen/status bar
						}
						
						last.x = (int)ev.getX();
						last.y = (int)ev.getY();
					}
				break;
				
				case MotionEvent.ACTION_UP: //Untouched the screen
					if (touched){
						touched = false;
					}
				break;
			}
		}
		
		return true;
	}
	
	public void createTurret(Point tile, int type){
		int player = 0;
		for (int i = 0; i < turretPoints.size(); i++){
			if(turretPoints.elementAt(i).getTile().equals(tile)){
				TurretPoint temp = turretPoints.remove(i);
				player = temp.getPlayer(); //Remove the TP and return its player
				temp.destroy();
				break;
			}
		}
		
		addTurret(tile, player, type);
	}
	
	public void sellTurret(Point tile){
		int player = 0;
		for (int i = 0; i < turrets.size(); i++){
			if(turrets.elementAt(i).getTile().equals(tile)){
				Turret temp = turrets.remove(i);
				player = temp.getPlayer();
				temp.destroy();
			}
		}

		addTurretPoint(tile, player);
	}
	public void upgradeTurret(Point tile){
		for (int i = 0; i < turrets.size(); i++){
			if(turrets.elementAt(i).getTile().equals(tile)){
				turrets.elementAt(i).levelUp();
				return;
			}
		}
	}
	
	public void addTurretPoint(Point tile, int player){
		Point p = new Point((int) (TILE_WIDTH*tile.x), (int)(TILE_HEIGHT*tile.y));
		TurretPoint tp = new TurretPoint(player, bmpTurretPoint, p, tile, insideHandler);
		turretPoints.add(tp);
		tp = null;
	}
	
	public void addTurret(Point tile, int player, int type){
		Point p = new Point((int) (TILE_WIDTH*tile.x+5), (int)(TILE_HEIGHT*tile.y+5));
		Turret turret = new Turret(player, p, tile, insideHandler, type);
		turrets.add(turret);
		turret = null;
	}
}
